using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace FinalGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        MouseState lastMouseState;
        SpriteFont spriteFont;
        CModel cmodel;
        Camera camera;
        Terrain terrain;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
        }
        // Called when the game should load its content
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Model model = Content.Load<Model>("Tank");
            cmodel = new CModel(model, new Vector3(0, 0, 0), Vector3.Zero, Vector3.One, GraphicsDevice);
            Effect effect = Content.Load<Effect>("SimpleEffect");
           // EffectPool ep = new EffectPool();
            lastMouseState = Mouse.GetState();
           // Effect effect = new BasicEffect(GraphicsDevice, ep);
            cmodel.SetModelEffect(effect, true);
            camera = new FreeCamera(new Vector3(0, 2000, 0),
                    MathHelper.ToRadians(0), // Turned around 153 degrees
                    MathHelper.ToRadians(0), // Pitched up 13 degrees
                    GraphicsDevice);
           // camera = new TargetCamera(new Vector3(0,2000,0),new Vector3(400,0,-400),GraphicsDevice);
            spriteFont = Content.Load<SpriteFont>("Font1");
                

            terrain = new Terrain(Vector3.Zero,Vector3.One,Vector3.One,GraphicsDevice,Content.Load<Effect>("effects"),Content.Load<Texture2D>("grass"));
            terrain.LoadMapFromTexture(Content.Load<Texture2D>("heightmap"));
            

        }
        // Called when the game should update itself
        protected override void Update(GameTime gameTime)
        {
            
            camera.Update();
            updateCamera(gameTime);
            base.Update(gameTime);
        }

        void updateCamera(GameTime gameTime)
        {
            // Get the new keyboard and mouse state
            MouseState mouseState = Mouse.GetState();
            KeyboardState keyState = Keyboard.GetState();
            // Determine how much the camera should turn
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                float deltaX = (float)lastMouseState.X - (float)mouseState.X;
                float deltaY = (float)lastMouseState.Y - (float)mouseState.Y;

                // Rotate the camera
                ((FreeCamera)camera).Rotate(deltaX * .01f, deltaY * .01f);
            }
            Vector3 translation = Vector3.Zero;
            // Determine in which direction to move the camera
            if (keyState.IsKeyDown(Keys.W)) translation += 0.1f * Vector3.Forward;
            if (keyState.IsKeyDown(Keys.S)) translation += 0.1f * Vector3.Backward;
            if (keyState.IsKeyDown(Keys.A)) translation += 0.1f * Vector3.Left;
            if (keyState.IsKeyDown(Keys.D)) translation += 0.1f * Vector3.Right;
            // Move 3 units per millisecond, independent of frame rate
            translation *= 1 * (float)gameTime.ElapsedGameTime.
            TotalMilliseconds;
            // Move the camera
            ((FreeCamera)camera).Move(translation);
            // Update the camera
            camera.Update();
            // Update the mouse state
            lastMouseState = mouseState;
        }
        // Called when the game should draw itself
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            // Calculate the starting world matrix
            //cmodel.Draw(camera.View, camera.Projection,((FreeCamera)camera).Position);
            terrain.Draw(camera.View,camera.Projection);
            spriteBatch.Begin();
            spriteBatch.DrawString(spriteFont,"Camera:"+((FreeCamera)camera).Position.ToString(), new Vector2(0, 0), Color.White);
            spriteBatch.DrawString(spriteFont,"Target:"+ ((FreeCamera)camera).Target.ToString(), new Vector2(0, 50), Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
